<?php

namespace Admin\Controllers\Auth;

use Admin\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Platform\System\Tools\Sms\AliyunSms\SendSms;
use Platform\Repository\Admin\AdminRepository;
use Platform\System\Tools\ResultTool;

class ForgotPasswordController extends BaseAdminController
{

    protected $repository;
    protected $sendSms;

    public function __construct(AdminRepository $adminRepository,SendSms $sendSms)
    {
        parent::__construct();
        $this->repository = $adminRepository;
        $this->sendSms = $sendSms;
    }

    /**
     * 获取短信验证码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCode(Request $request){
        $phone = $request->phone;
        if (!$phone) {
            return myResult()->setCode(ResultTool::CODE_PARAM_LOST,'手机号不可为空')->send();
        }
        if (!preg_match($this->phone_preg,$phone)) {
            return myResult()->setCode(ResultTool::CODE_PARAM_WRONG,'手机号格式不正确')->send();
        }
        $admin = $this->repository->getOneByPhone($phone);
        if (!$admin){
            return myResult()->setCode(ResultTool::CODE_TARGET_WRONG,'该手机号码不存在')->send();
        }

        try{
            $code = randomKeys(6);
            $res = $this->sendSms->sendSms($phone,'forget',['code'=>$code]);

            if($res->Code == 'OK') {
                $this->sendSms->cacheCode($phone,$code,'forget',5);
                return myResult()->setCode(ResultTool::CODE_SUCCESS,'发送成功')->send();
            }else throw new \Exception('发送失败',ResultTool::CODE_TARGET_WRONG);

        }catch (\Exception $e){
            mlog('send_sms_error_log',$phone);
            mlog('send_sms_error_log',$e->getMessage());
            return myResult()->setCode(ResultTool::CODE_TARGET_WRONG,'发送失败')->send();
        }

    }


    /**
     * 检查短信验证码以及修改用户密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkCode(Request $request){
        $phone = $request->phone;
        if (!$phone) {
            return myResult()->setCode(ResultTool::CODE_PARAM_LOST,'手机号不可为空')->send();
        }
        $code = $request->code;
        if (!$code) {
            return myResult()->setCode(ResultTool::CODE_PARAM_LOST,'验证码不可为空')->send();
        }
        $password = $request->password;
        if (!$password) {
            return myResult()->setCode(ResultTool::CODE_PARAM_LOST,'密码不可为空')->send();
        }

        $checkRes = $this->sendSms->checkCode($phone,$code,'forget');
        if (!$checkRes) {
            return myResult()->setCode(ResultTool::CODE_TARGET_WRONG,'验证码错误')->send();
        }

        $admin = $this->repository->getOneByPhone($phone);
        if (!$admin){
            return myResult()->setCode(ResultTool::CODE_TARGET_WRONG,'该手机号码不存在')->send();
        }

        $admin->password = Hash::make($password);
        $admin->save();
        return myResult()->setCode(ResultTool::CODE_SUCCESS,'密码重置成功')->send();
    }
}
